﻿<%@ WebHandler Language="C#" Class="updatePwd" %>


using System;
using System.Web;
using System.Data.OleDb;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using System.Xml.Linq;
using System.Web.Script.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.SessionState;//第一步：导入此命名空间

public class updatePwd : IHttpHandler, IRequiresSessionState{
    
    public void ProcessRequest (HttpContext context) {
        //指定回送数据的类型
        context.Response.ContentType = "application/json";

        string oldpwd = context.Request["oldpwd"],
        newpwd = context.Request["newpwd"],
        confirm_newpwd = context.Request["confirm_newpwd"],
        user_id = Convert.ToString(context.Session["user_id"]);



        //准备回送的字符串
        string outstr = "";

        //执行写入操作
        outstr = doUpdate(oldpwd, newpwd, confirm_newpwd, user_id);

        //回送结果
        context.Response.Write(outstr);
    }
    public string doUpdate(string oldpwd, string newpwd, string confirm_newpwd, string user_id)
    {
        string msg = "操作成功";
        int r = 0;
        //执行查询操作，并获得返回的行数
        DataSet isHave = DBHelper.sqlMethod("select * from admins where id ='" + user_id + "' and password = '" + oldpwd + "'");
        int i = isHave.Tables[0].Rows.Count;
        if (i == 0)
        {
            msg = "原密码错误";
        }
        else if (newpwd != confirm_newpwd)
        {
            msg = "两次密码不匹配";
        }
        else
        {
            r = DBHelper.sqlMethodNoDataset("update admins set password = '" + newpwd + "' where id = '" + user_id + "'");
        }
        
        StringBuilder Builder = new StringBuilder();
        Builder.Append("{");
        Builder.Append("\"r\":" + r + "");
        Builder.Append(",\"data\":\"" + msg + "\"");
        Builder.Append("}");
        return Builder.ToString();

    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}